package it.gameplanet.client.widgets.news;

import it.gameplanet.client.utils.AbstractAsyncCallback;
import it.gameplanet.client.widgets.text.RichTextToolbar;
import it.gameplanet.model.News;
import it.gameplanet.service.GamePlanetService;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

public class EditNewsDialog extends DialogBox {

	private News news;
	
	final TextBox titolo = new TextBox();
	final DateBox date = new DateBox();
	final RichTextArea testo = new RichTextArea();
	
	public EditNewsDialog(Command callback) {
		super();
		
		initEditDialog(callback);
	}

	private void initEditDialog(final Command callback) {

		this.setText("Modifica/Inserisci News");
		this.setAnimationEnabled(true);
		VerticalPanel dialogVPanel = new VerticalPanel();
		
		dialogVPanel.add(new HTML("<b>Titolo:</b>"));
		dialogVPanel.add(titolo);
		dialogVPanel.add(new HTML("<b>Data:</b>"));
		date.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getShortDateFormat()));
		dialogVPanel.add(date);
		dialogVPanel.add(new HTML("<b>Testo:</b>"));
		
		testo.ensureDebugId("cwRichText-area");
		testo.setSize("100%", "14em");
	    RichTextToolbar toolbarDescrizione = new RichTextToolbar(testo);
	    toolbarDescrizione.ensureDebugId("cwRichText-toolbar");
	    toolbarDescrizione.setWidth("100%");

	    // Add the components to a panel
	    Grid gridDescrizione = new Grid(2, 1);
	    gridDescrizione.setStyleName("cw-RichText");
	    gridDescrizione.setWidget(0, 0, toolbarDescrizione);
	    gridDescrizione.setWidget(1, 0, testo);

	    dialogVPanel.add(gridDescrizione);

	    HorizontalPanel bottoniera = new HorizontalPanel();
	    bottoniera.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    
		Button inserisciButton = new Button("OK");
		inserisciButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				news.setTitolo(titolo.getText());
				news.setDataPubblicazione(date.getDatePicker().getValue());
				news.setTesto(testo.getHTML());
				
				GamePlanetService.Utils.getGamePlanetServiceAsync().inserisciNews(news, new AbstractAsyncCallback<Void>(){
					@Override
					public void onSuccess(Void result) {
						hide();
						callback.execute();
					}});
			}
		});		
		bottoniera.add(inserisciButton);
		
		Button annullaButton = new Button("Annulla");
		annullaButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});		
		bottoniera.add(annullaButton);
		
		dialogVPanel.add(bottoniera);

	    this.setWidget(dialogVPanel);
	}
	
	public void editNews(News newsP) {
		
		this.news = newsP;
		
		syncNews();
		
		show();
		center();
	}

	private void syncNews() {
		titolo.setText(news.getTitolo());
		date.getDatePicker().setValue(news.getDataPubblicazione());
		testo.setHTML(news.getTesto());
	}
}
